package com.phei.netty.codec.protobuf;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangyingxuan
 */
public class SubReqClientHandler extends ChannelHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        for (int i = 0; i < 10; i ++) {
            SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();
            builder.setSubReqID(i);
            builder.setUserName("LiLinFeng");
            builder.setProductName("Netty Book");
            List<String> address = new ArrayList<>(4);
            address.add("NanJing YuHuaTai");
            address.add("Hanzhou yanyuxiang");
            builder.addAllAddress(address);

            SubscribeReqProto.SubscribeReq subscribeReq = builder.build();

            ctx.writeAndFlush(subscribeReq);
        }
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println(msg);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
